package com.bocai.yuweiwangluo.activity;


import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bocai.yuweiwangluo.R;
import com.bocai.yuweiwangluo.base.BaseActivity;
import com.bocai.yuweiwangluo.utils.ImagePathInitUtil;
import com.bocai.yuweiwangluo.utils.ShareUtil;
import com.bocai.yuweiwangluo.utils.UrlData;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;

/**
 * 场馆预订界面
 */
public class VenueBookActivity extends BaseActivity implements View.OnClickListener
{
    @ViewInject(R.id.webview)
    WebView webView;
    @ViewInject(R.id.ll_refresh)
    LinearLayout llRefresh;
    private ShareUtil shareUtil;
    private String id;
    private String title;
    private String content;
    private String shareImagePath="";
    private String url="";
    private String shareUrl="";
    private AlertDialog dialog;
    private String local_x;
    private String local_y;
    @Override
    protected int contentView()
    {
        return R.layout.activity_venue_book;
    }

    @Override
    protected void setData()
    {
        ViewUtils.inject(this);
        initToolbar("场地");
        mToolbar.inflateMenu(R.menu.menu_game_detail);
        id= getIntent().getStringExtra("id");
        title=getIntent().getStringExtra("title");
        content=getIntent().getStringExtra("content");
        local_x=getIntent().getStringExtra("local_x");
        local_y=getIntent().getStringExtra("local_y");
        url=UrlData.IMAGEURL+"/page/place?id=" + id;
        shareUrl=UrlData.IMAGEURL+"/page/place_share?id="+id+"&s=android";
        initEvent();
        initData();
    }

    @Override
    protected void initEvent()
    {
        llRefresh.setOnClickListener(this);
        mToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                View view = LayoutInflater.from(VenueBookActivity.this).inflate(R.layout.dialog_share, null);
                dialog = new AlertDialog.Builder(VenueBookActivity.this)
                        .setView(view)
                        .show();
                setDialogView(view);
                return true;
            }
        });
        new Thread(new Runnable() {
            @Override
            public void run() {
                shareImagePath=ImagePathInitUtil.initImagePath(VenueBookActivity.this);
            }
        }).start();
    }

    private void initData() {
        webView.loadUrl(url);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(true);
        webView.getSettings().setBuiltInZoomControls(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

                String[] urls = url.split("add?");
                if (url.endsWith("add")) {
                    //没有任何选择
                    Toast.makeText(VenueBookActivity.this, "您没选择任何场馆", Toast.LENGTH_SHORT).show();

                } else if(url.startsWith(UrlData.IMAGEURL+"/upload/pic")){
                    Intent intent=new Intent(VenueBookActivity.this,BigPicActivity.class);
                    intent.putExtra("picUrl",url);
                    startActivity(intent);
                }else if (url.endsWith("page/site")) {
                    Intent intent = new Intent(VenueBookActivity.this, VenueDetailActivity.class);
                    intent.putExtra("id", id);
                    intent.putExtra("local_x", local_x);
                    intent.putExtra("local_y", local_y);
                    startActivity(intent);
                } else if (url.endsWith("page/site?id=" + id)) {
                    Intent intent = new Intent(VenueBookActivity.this, VenueDetailActivity.class);
                    intent.putExtra("local_x", local_x);
                    intent.putExtra("local_y", local_y);
                    intent.putExtra("id", id);
                    startActivity(intent);
                } else if (url.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(url));
                    startActivity(intent);
                } else if (!TextUtils.isEmpty(urls[1])) {
                    //跳转至支付页面
                    Intent intent = new Intent(VenueBookActivity.this, OrderDetailActivity.class);
                    intent.putExtra("content", urls[1]);
                    intent.putExtra("id", id);
                    startActivityForResult(intent, 1000);
                } else {
                    webView.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                llRefresh.setVisibility(View.VISIBLE);
                Toast.makeText(VenueBookActivity.this, "网页加载出错！", Toast.LENGTH_LONG).show();

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1000){
            if(resultCode==2000){
                initData();
            }

        }
    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.ll_refresh:
                llRefresh.setVisibility(View.GONE);
                initData();
                break;
            case R.id.btn_order:
                startActivity(new Intent(this,OrderDetailActivity.class));
                break;
            case R.id.ib_title_right:
                View view = LayoutInflater.from(this).inflate(R.layout.dialog_share, null);
                dialog = new AlertDialog.Builder(this)
                        .setView(view)
                        .show();
                setDialogView(view);
                break;
            case R.id.ll_dialog_qq:
                shareUtil.showShare(this,dialog,"qq",shareImagePath,
                        title,content,shareUrl);
                break;
            case R.id.ll_dialog_weixin:
                shareUtil.showShare(this,dialog,"weixin",shareImagePath,
                        title,content,shareUrl);
                break;
            case R.id.ll_dialog_weixinfriend:
                shareUtil.showShare(this,dialog,"weixinfriend",shareImagePath,
                        title,content,shareUrl);
                break;
            case R.id.ll_dialog_sinaweibo:
                shareUtil.showShare(this,dialog,"sinaweibo",shareImagePath,
                        title,content,shareUrl);
                break;
        }
    }

    private void setDialogView(View view)
    {
        shareUtil=new ShareUtil();
        LinearLayout ll_qq=(LinearLayout)view.findViewById(R.id.ll_dialog_qq);
        LinearLayout ll_weixin=(LinearLayout)view.findViewById(R.id.ll_dialog_weixin);
        LinearLayout ll_weixinfriend=(LinearLayout)view.findViewById(R.id.ll_dialog_weixinfriend);
        LinearLayout ll_sianweibo=(LinearLayout)view.findViewById(R.id.ll_dialog_sinaweibo);
        ll_weixin.setOnClickListener(this);
        ll_weixinfriend.setOnClickListener(this);
        ll_qq.setOnClickListener(this);
        ll_sianweibo.setOnClickListener(this);
    }
}
